package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

@MessageTag(value = IMMessageType.TIP_SHOW_MSG ,flag = MessageTag.ISPERSISTED)
public class RXShowTipMessageContent extends MessageContent {

    private String content;
    private Map<String,Object> extra;

    public static RXShowTipMessageContent setCustomTipMessage(String content, Map<String,Object>  extra){
        RXShowTipMessageContent mRXMessageContent =new RXShowTipMessageContent();
        mRXMessageContent.content =content;
        mRXMessageContent.extra = extra;
        return mRXMessageContent;
    }

    @Override
    public byte[] encode() {
        JSONObject object =new JSONObject();
        object.put("content",content);
        object.put("extra",extra);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXShowTipMessageContent() {
    }

    public RXShowTipMessageContent(byte[] data) {
        super(data);
        String jsonStr=null;
        try {
            jsonStr =new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);

            setContent(object.getString("content"));
            setMyExtra(JSON.parseObject(object.getString("extra")));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);
        ParcelUtils.writeToParcel(dest,extra);
    }

    public static final Creator<RXShowTipMessageContent> CREATOR = new Creator<RXShowTipMessageContent>() {

        @Override
        public RXShowTipMessageContent createFromParcel(Parcel source) {
            return new RXShowTipMessageContent(source);
        }

        @Override
        public RXShowTipMessageContent[] newArray(int size) {
            return new RXShowTipMessageContent[size];
        }
    };


    public RXShowTipMessageContent(Parcel parcel){

        content = ParcelUtils.readFromParcel(parcel);
        extra = ParcelUtils.readMapFromParcel(parcel);
    }

    @Override
    public int describeContents() {
        return 0;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Map<String, Object> getMyExtra() {
        return extra;
    }

    public void setMyExtra(Map<String, Object> map){
        this.extra = map;
    }

}
